package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.bean.BaseParam;
import com.cskaoyan.catmall.dao.WxFootPrintMapper;
import com.cskaoyan.catmall.model.vo.WxFootListVo;
import com.cskaoyan.catmall.service.WxFootPrintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 创建日期: 2022-01-10-14-05
 * author : WQW
 * description: 足迹信息的查询和删除
 */
@Service
public class WxFootPrintServiceImpl implements WxFootPrintService {


    @Autowired
    WxFootPrintMapper wxFootPrintMapper;


    /**
     * @param
     * @param baseParam
     * @return java.util.List<com.cskaoyan.catmall.model.vo.WxFootListVo>
     * @description 在数据库中查询足迹相关信息，并进行分页，封装成对应Vo返回
     * @author WQW
     * @date 2022/1/10 14:17
     */
    @Override
    public WxFootListVo getAllFootPrint(BaseParam baseParam, Integer userId) {
        Integer page = baseParam.getPage();// 页码
        Integer limit = baseParam.getLimit(); // 每页数据量
        PageHelper.startPage(page, limit);
        List<WxFootListVo.ListDTO> allFootPrintList = wxFootPrintMapper.selectAllFootPrint(userId);
        PageInfo<WxFootListVo.ListDTO> listDTOPageInfo = new PageInfo<>(allFootPrintList);
        int pages = listDTOPageInfo.getPages(); // 总页数
        long total = listDTOPageInfo.getTotal(); // 总数据量
        return new WxFootListVo(total, pages, limit, page, allFootPrintList);
    }


    /**
     * @param
     * @param id
     * @return int
     * @description 根据足迹id，在数据库中，将对应足迹信息删除
     * @author WQW
     * @date 2022/1/11 14:10
     */
    @Transactional
    @Override
    public int deleteFootPrint(Integer id) {
        int result = wxFootPrintMapper.deleteFootPrintById(id);
        // 如果返回影响行数为1，说明成功删除返回200，否则删除结果异常，返回404
        if (result == 1) {
            return 200;
        }
        return 404;
    }
}
